//
//  WHPreviewImageView.m
//  WHEasyBizLink
//
//  Created by shengmeng chen on 2024/11/27.

#import "WHPreviewImageView.h"
#import <Photos/Photos.h>
#import "WSLPhotoZoom.h"
#import "WHDialog.h"

#define showHeight  ScreenH*2/3

@interface WHPreviewImageView ()<UIScrollViewDelegate>

@property (nonatomic,weak) UIScrollView *scrollView;
@property (nonatomic,weak) UILabel *label;
@property (nonatomic,assign) float imageW;
@property (nonatomic,assign) float imageH;
@property (nonatomic,assign) CGSize selfSize;
@property (nonatomic,assign) int page;
@property (nonatomic,strong) NSMutableArray *imageViewArray;
@property (nonatomic,strong) NSMutableArray *zoomViewArray;

@property(nonatomic,strong)NSString*urlString;

@end

@implementation WHPreviewImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setViewChilds];
    }
    return self;
}
-(void)setViewChilds{
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,40, ScreenW, showHeight)];
    _scrollView = scrollView;
    scrollView.delegate = self;
    scrollView.showsVerticalScrollIndicator = NO;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.pagingEnabled = YES;//允许分页滑动
    scrollView.bounces = NO;//到边了就不能再拖地
    [self addSubview:scrollView];
    
    UILabel*label = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW/2-40,CGRectGetMaxY(scrollView.frame),80,30)];
    label.textAlignment = NSTextAlignmentCenter;
    _label = label;
    
    [self addSubview:label];
    
    UIButton*changBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenW/2-50,CGRectGetMaxY(label.frame)+30,100,40)];
    [changBtn setTitle:@"转PDF" forState:UIControlStateNormal];
    [changBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    changBtn.backgroundColor =RGBColor(106, 168, 255);
    changBtn.titleLabel.font =[UIFont systemFontOfSize:14];
    
    changBtn.layer.cornerRadius  = 5;
    changBtn.layer.masksToBounds = YES;
    
   [changBtn addTarget:self action:@selector(changBtn:)
    forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:changBtn];
}

-(void)setImgArray:(NSMutableArray<UIImage *> *)imgArray
{
    _imgArray = imgArray;
    CGSize selfSize = self.frame.size;
    _selfSize = selfSize;

    NSMutableArray *imageViewArray = [[NSMutableArray alloc]init];
    NSMutableArray *zoomViewArray = [[NSMutableArray alloc]init];
    
    for (int i = 0; i < imgArray.count; i++) {
        
        WSLPhotoZoom *zoomView = [[WSLPhotoZoom alloc]initWithFrame:Frame(i * _selfSize.width, 0, _selfSize.width, showHeight)];
        zoomView.delateBtn.hidden = NO;
        zoomView.delateBtn.tag    = i;
        [zoomView.delateBtn addTarget:self action:@selector(btnClicked:)forControlEvents:UIControlEventTouchUpInside];
        float imageW;
        float imageH;
        UIImage *image =imgArray[i];
        CGSize pictureSize = image.size;
        if (pictureSize.width > selfSize.width || pictureSize.height > selfSize.height) {
            if(pictureSize.height >= pictureSize.width){
                float clickW = pictureSize.width * selfSize.height/pictureSize.height;
                imageH = selfSize.height;
                imageW = clickW;
            }else{
                float clickH = selfSize.height * pictureSize.height/pictureSize.width;
                imageW = selfSize.width;
                imageH = clickH;
            }
        }else{
            imageW = pictureSize.width;
            imageH = pictureSize.height;
        }
        
       _imageH = imageH;
       _imageW = imageW;
        zoomView.imageView.image = image;
       //自适应图片宽高比例
        zoomView.imageView.contentMode = UIViewContentModeScaleAspectFit;
        zoomView.imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        zoomView.imageView.clipsToBounds  = YES;
        
        [imageViewArray addObject:zoomView.imageView];
        [zoomViewArray addObject:zoomView];
        [_scrollView addSubview:zoomView];
    }
    
    _imageViewArray = imageViewArray;
    _zoomViewArray  = zoomViewArray;
    _scrollView.contentSize = CGSizeMake(_selfSize.width * imgArray.count, showHeight);
    if(self.imgArray.count>0){
        self.label.text = [NSString stringWithFormat:@"%d/%lu",1,(unsigned long)self.imgArray.count];
    }else{
        self.label.text = [NSString stringWithFormat:@"%d/%lu",0,(unsigned long)self.imgArray.count];
    }
    
}

- (void)setIndexpath:(int)indexpath{
    _indexpath = indexpath;
    _page = indexpath;
    _scrollView.contentOffset = CGPointMake(_selfSize.width*indexpath, 0);
    self.label.text = [NSString stringWithFormat:@"%d/%lu",_page+1,(unsigned long)self.imgArray.count];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    int page = floor((scrollView.contentOffset.x - _selfSize.width/2)/_selfSize.width) +1;
   
    NSLog(@"asdasda长度%f",scrollView.contentOffset.x);
    NSLog(@"asdasda这是第%i页",page);
    self.label.text = [NSString stringWithFormat:@"%d/%lu",page+1,(unsigned long)self.imgArray.count];
    if (_page !=page) {
        
        for(int i=0;i<scrollView.subviews.count;i++)
        {
            //重置上一个缩放过的视图
            WSLPhotoZoom * zoomView  = (WSLPhotoZoom *)scrollView.subviews[i];
            [zoomView pictureZoomWithScale:1.0];
        }
    }
    _page = page;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger scrollIndex = round(scrollView.contentOffset.x / scrollView.frame.size.width);
    if (scrollIndex != self.page) {
        //重置上一个缩放过的视图
        WSLPhotoZoom * zoomView  = (WSLPhotoZoom *)scrollView.subviews[self.page];
        [zoomView pictureZoomWithScale:1.0];
        self.page = (int)scrollIndex;
    }
}
-(void)btnClicked:(UIButton*)btn
{
    if (self.clickBtnBlock) {
        self.clickBtnBlock(WHPreviewBtnClickDelate, (int)btn.tag);
    }
}
-(void)changBtn:(UIButton*)btn
{
    if (self.clickBtnBlock) {
        if(self.imgArray.count>0){
            self.clickBtnBlock(WHPreviewBtnClickChange,0);
        }else{
            [[WHDialog sharedManager] showHudAlertWithText:@"请先去扫描文件!"];
        }
    }
}
-(void)removeScrollViewSubView
{
    for(int i=0;i<self.zoomViewArray.count;i++)
    {
        WSLPhotoZoom *zoomView =self.zoomViewArray[i];
        [zoomView removeFromSuperview];
    }
}

@end
